﻿package mortal.game.view.halidom
{
    import Message.Client.*;
    import Message.DB.Tables.*;
    import Message.Game.*;
    import mortal.component.window.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class HalidomController extends Controller
    {
        private var _module:HalidomModuleNew;

        public function HalidomController()
        {
            return;
        }// end function

        private function onIconClick(event:DataEvent) : void
        {
            if (view.isHide)
            {
                view.show();
            }
            else
            {
                view.hide();
            }
            return;
        }// end function

        private function onIconShow(param1) : void
        {
            if (cache.role.entityInfo.level >= 22)
            {
            }
            return;
        }// end function

        private function onInfoReq(event:DataEvent) : void
        {
            if (cache.role.entityInfo.level >= GameDefConfig.instance.getSystemOpenLevel(10))
            {
                GameProxy.role.getHalidomInfo();
            }
            return;
        }// end function

        private function onInfoRes(param1:Object) : void
        {
            if (this._module && !this._module.isHide)
            {
                this._module.onHalidomInfoRes();
            }
            this.searchCanUpgrade();
            return;
        }// end function

        private function onBackPackItemDel(param1:Object) : void
        {
            var _loc_2:* = param1 as ItemData;
            if (ItemsUtil.isHalidom(_loc_2))
            {
                if (this._module && !this._module.isHide)
                {
                    this._module.onHalidomInfoRes(true);
                }
                this.searchCanUpgrade();
            }
            return;
        }// end function

        private function onBackPackItemsReduction(param1:Object) : void
        {
            var _loc_2:* = param1[0] as ItemData;
            this.onBackPackItemDel(_loc_2);
            return;
        }// end function

        private function searchCanUpgrade() : void
        {
            var _loc_2:THalidomConfig = null;
            var _loc_3:THalidomConfig = null;
            var _loc_4:SHalidom = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:Boolean = false;
            var _loc_10:THalidomSeriesConfig = null;
            var _loc_11:THalidomSeriesConfig = null;
            var _loc_12:Array = null;
            var _loc_13:int = 0;
            var _loc_1:Array = [];
            var _loc_9:* = HalidomConfig.instance.seriesData;
            for each (_loc_10 in _loc_9)
            {
                
                _loc_12 = HalidomConfig.instance.getHalidomListBySeries(_loc_10.series);
                _loc_1 = _loc_1.concat(_loc_12);
            }
            for each (_loc_11 in _loc_1)
            {
                
                _loc_4 = cache.halidom.getItemById(_loc_11.halidomId);
                _loc_6 = _loc_4 ? (_loc_4.pieces) : (0);
                _loc_7 = _loc_4 && _loc_4.level > 0 ? (_loc_4.level) : (1);
                _loc_2 = HalidomConfig.instance.getItem(_loc_11.halidomId, _loc_7);
                _loc_3 = HalidomConfig.instance.getItem(_loc_11.halidomId, (_loc_7 + 1));
                _loc_5 = ItemsUtil.getUnbindCode(_loc_2.itemCode);
                _loc_13 = cache.pack.backPackCache.getItemCountByItemCode(_loc_5);
                if (_loc_13 >= _loc_2.itemNum - _loc_6 && _loc_3)
                {
                    _loc_8 = true;
                    break;
                }
            }
            GameController.mapNavBar.rightTopBar.blinkBtn("halidom", _loc_8);
            return;
        }// end function

        private function onBackPackItemAdd(param1:Object) : void
        {
            var _loc_2:* = param1 as ItemData;
            if (ItemsUtil.isHalidom(_loc_2))
            {
                if (this._module && !this._module.isHide)
                {
                    this._module.onHalidomInfoRes(true);
                }
                this.searchCanUpgrade();
            }
            return;
        }// end function

        private function onBackPackItemsAdd(param1:Object) : void
        {
            this.onBackPackItemAdd(ItemData(param1[0]));
            return;
        }// end function

        private function onOper(event:DataEvent) : void
        {
            var _loc_2:* = event.data.type;
            var _loc_3:* = event.data.id;
            if (_loc_2 == EHalidomOperType._EHalidomOperTypeSubmit)
            {
                GameProxy.role.commitHalidomPieces(_loc_3);
            }
            else if (_loc_2 == EHalidomOperType._EHalidomOperTypeUpgrade)
            {
                GameProxy.role.upgradeHalidom(_loc_3);
            }
            return;
        }// end function

        private function onShow(event:WindowEvent) : void
        {
            Dispatcher.addEventListener(EventName.HalidomOper, this.onOper);
            return;
        }// end function

        private function onClose(event:WindowEvent) : void
        {
            Dispatcher.removeEventListener(EventName.HalidomOper, this.onOper);
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.HalidomInfoRes, this.onInfoRes);
            Dispatcher.addEventListener(EventName.HalidomIconClick, this.onIconClick);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.onIconShow);
            Dispatcher.addEventListener(EventName.HalidomInfoReq, this.onInfoReq);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.onInfoReq);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemDel, this.onBackPackItemDel);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsReduction, this.onBackPackItemsReduction);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemAdd, this.onBackPackItemAdd);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsAdd, this.onBackPackItemsAdd);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onIconShow);
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._module == null)
            {
                this._module = new HalidomModuleNew();
                this._module.addEventListener(WindowEvent.SHOW, this.onShow);
                this._module.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._module;
        }// end function

    }
}
